package com.bro.person.admin.controller;

import com.bro.person.admin.struct.SecurityUserStruct;
import com.bro.person.base.constants.ConstConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author swallow
 * Created by swallow on 2018/11/15.<br>
 */
@Controller
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping(value = {"","/","/index"})
    public String index(Model model, HttpServletRequest request){
        //从 cookie中获取用户参数
        HttpSession session = request.getSession();
        SecurityUserStruct userStruct = (SecurityUserStruct)session.getAttribute(ConstConfig.SECURITY_USER_SESSION_KEY);
        model.addAttribute("user",userStruct);
        return "index";
    }

    /**
     * 普通页面访问方式 只能放共有的页面 传输 其他页面不放入到 pages目录中
     * @param path
     * @return
     */
    @RequestMapping(value = "/pages/{path}")
    public String toPath(@PathVariable(value = "path") String path){
        return "pages/" + path;
    }

    /**
     * 普通页面访问方式 只能放共有的页面 传输 其他页面不放入到 pages目录中
     * @param path
     * @return
     */
    @RequestMapping(value = "/pages/{model}/{path}")
    public String toModelPath(@PathVariable(value = "model") String model,@PathVariable(value = "path") String path){
        return "pages/" + model + "/" +path;
    }

    @RequestMapping(value = "/loginout")
    public String loginout(HttpServletRequest request, HttpServletResponse response){
        logger.debug("[IndexController][loginout] entering request:{},response:{}",request, response);
        HttpSession session = request.getSession();
        session.invalidate();
        return "/pages/login";
    }
}
